package org.oneblog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.oneblog.data.bean.AlbumSubs;
import org.oneblog.data.bean.AlbumView;
import org.oneblog.data.query.AlbumQuery;
import org.oneblog.data.query.ArticleSolrQuery;
import org.oneblog.data.rs.Result;
import org.oneblog.data.util.StringTools;
import org.oneblog.service.IAlbumService;
import org.oneblog.service.IArticleService;
import org.oneblog.view.ArticleView;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller("PageController")
@RequestMapping("/")
public class PageController extends BaseController {

    //公用
    @RequestMapping(value = "/top")
    public ModelAndView top(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("common/top");
        
        return modelAndView;
    }

    @RequestMapping(value = "/bottom")
    public ModelAndView bottom(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("common/bottom");
        
        return modelAndView;
    }

    //公用
    @RequestMapping(value = "/redirect")
    public ModelAndView errorRedirect(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("error_redirect");
        
        return modelAndView;
    }
    
    @RequestMapping(value = "/home")
    public ModelAndView error(HttpServletRequest request,
            @ModelAttribute ArticleSolrQuery query) {
        ModelAndView modelAndView = new ModelAndView("index");
        
        Result<ArticleView> articles = articleService.queryArticles(new ArticleSolrQuery());

        AlbumQuery albumQuery = new AlbumQuery();
        albumQuery.setViewDefuncted(0);
        albumQuery.setPageNo(1);
        albumQuery.setPageSize(10);
        albumQuery.setViewSubs(0);
        articles.setAlbums(albumService.getAlbums(albumQuery));
        
        request.setAttribute("articles", articles);
        
        request.setAttribute("query", query);
        
        return modelAndView;
    }

    @RequestMapping(value = "/list")
    public ModelAndView list(HttpServletRequest request,
            @ModelAttribute ArticleSolrQuery query) {
        ModelAndView modelAndView = new ModelAndView("article");
        
        Result<ArticleView> articles = articleService.queryArticles(query);
        
        if(CollectionUtils.isEmpty(articles.getList())) {
            articles.setRecommend(1);
            articles.setList(articleService.priorityArticles());
//        } else {
//            for(ArticleView v : articles.getList()) {
//                System.out.println(v.getContent());
//                System.out.println();
//                System.out.println("==========================================================");
//                System.out.println("==========================================================");
//                System.out.println();
//            }
        }
        
        request.setAttribute("articles", articles);
        request.setAttribute("categoryFacet", articleService.categoryFacet());
        //公共信息
        setAttributes(request, null);
        
        request.setAttribute("query", query);
        
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("key", 1);
        modelAndView.addAllObjects(modelMap);
        
        return modelAndView;
    }
    

    @RequestMapping(value = "/album")
    public ModelAndView album(HttpServletRequest request,
            @ModelAttribute AlbumView query) {
        ModelAndView modelAndView = new ModelAndView("album");

        AlbumView albumView = albumService.detail(query.getId());
        
        if(albumView != null) {
            List<AlbumSubs> articles = albumView.getArticles();
            if(articles != null && articles.size() > 0) {
                for(AlbumSubs s : articles) {
                    String c = s.getContent();
                    c = StringTools.delHTMLTag(c);
                    s.setContent(c);
                }
            }
        }
        request.setAttribute("album", albumView);
        request.setAttribute("categoryFacet", articleService.categoryFacet());

        setAttributes(request, null);
        
        return modelAndView;
    }

    @RequestMapping(value = "/about")
    public ModelAndView about(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("about");
        
        return modelAndView;
    }

    @RequestMapping(value = "/articledetail")
    public ModelAndView articleDetail(HttpServletRequest request,
            @RequestParam(value = "atclid", defaultValue = "", required = true) String articleId) {
        ModelAndView modelAndView = new ModelAndView("article_detail");
        
        ArticleView detail = articleService.detail(articleId);
        detail.setGoodSet(isGood(request, articleId) ? 1 : 0);
        
        request.setAttribute("article", detail);
        
        //分类统计
        request.setAttribute("categoryFacet", articleService.categoryFacet());
        
        setAttributes(request, articleId);

        request.setAttribute("query", new ArticleSolrQuery());

        return modelAndView;
    }
    
    private void setAttributes(HttpServletRequest request, String id) {
        request.setAttribute("hotTags", articleService.allHotTags());
        request.setAttribute("hotArticle", articleService.hotArticles());
        request.setAttribute("priorityArticle", articleService.priorityArticles());
        request.setAttribute("relativeArticles", articleService.relativeArticles(id));
    }
    
    @Resource(name = "articleService")
    IArticleService articleService;
    @Resource(name = "albumService")
    private IAlbumService albumService;
}
